package com.jy.app.lib.adapter;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.jy.app.lib.R;
import com.jy.app.lib.utils.FindViewBindText;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jan on 2017/6/26.
 */
public class CommonListAdaptrer<E> extends CommonAdapter<E> {
    private View rightMenu;
    private View currentDeleteItem;
    private Animation mDelAnim;
    private OnConvertListener onConvertListener;
    private OnItemMenuOperateListener onItemMenuOperateListener;

    private boolean isAutoBind;


    public CommonListAdaptrer(Context activity, List<E> data, int layoutId, OnConvertListener onConvertListener) {
        super(activity, data, layoutId);
        this.onConvertListener = onConvertListener;
    }

    public CommonListAdaptrer(Context activity, List<E> data, int layoutId, boolean isAutoBind) {
        super(activity, data, layoutId);
        this.isAutoBind = isAutoBind;
    }

    public CommonListAdaptrer(Context activity, List<E> data, int layoutId, boolean isAutoBind, OnConvertListener onConvertListener) {
        super(activity, data, layoutId);
        this.isAutoBind = isAutoBind;
        this.onConvertListener = onConvertListener;
    }

    public CommonListAdaptrer(Context activity, List<E> data, int layoutId, OnConvertListener onConvertListener, OnItemMenuOperateListener onItemMenuOperateListener) {
        this(activity, data, layoutId, onConvertListener);
        this.onItemMenuOperateListener = onItemMenuOperateListener;
    }

    public CommonListAdaptrer(Context activity, int layoutId, OnConvertListener onConvertListener) {
        this(activity, new ArrayList<E>(), layoutId,onConvertListener);
    }

    public CommonListAdaptrer(Context activity, int layoutId,  boolean isAutoBind) {
        this(activity, new ArrayList<E>(), layoutId,isAutoBind);
    }

    protected void removeListItem(View rowView) {
        if (rowView == null) return;
        currentDeleteItem = rowView;
        mDelAnim = (Animation) AnimationUtils.loadAnimation(rowView.getContext(), R.anim.item_right_anim);
        rightMenu.setVisibility(View.INVISIBLE);
        mDelAnim.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                animation.cancel();
                if (onItemMenuOperateListener != null)
                    onItemMenuOperateListener.onItemDelCompleted();
            }
        });
    }

    public void deleteCompleted() {
        currentDeleteItem.startAnimation(mDelAnim);
    }


    @Override
    public void convert(final CommonViewHolder holder, final E e) {
        final int position = holder.getPosition();
        View view = holder.getConvertView();
        if(isAutoBind&&view!=null&&e!=null){
            new FindViewBindText().viewFindText(view,e);
        }
        if (onItemMenuOperateListener != null) {
            rightMenu = holder.getView("llayout_right");//R.id.llayout_right<=>llayout_right
            rightMenu.setVisibility(View.INVISIBLE);
            final View delView = holder.getView("rl_delete");
            if (delView != null) {
                delView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        View itemDelView = holder.getView("fl_item");
                        if(itemDelView!=null){
                            onItemMenuOperateListener.onItemDel(itemDelView, e);
                        }else {
                            onItemMenuOperateListener.onItemDel(delView, e);
                        }
                        removeListItem(delView);
                    }
                });
            }
            View editView = holder.getView("rl_edit");
            if(editView!=null){
                editView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemMenuOperateListener.onItemEdit(v, e);
                    }
                });
            }
        }
        if (onConvertListener != null) {
            onConvertListener.onConvert(holder, e);
        }
    }

    //item中的数据赋值
    public interface OnConvertListener<E> {
        void onConvert(CommonViewHolder holder, E e);
    }

    //侧滑菜单点击事件
    public interface OnItemMenuOperateListener<E> {
        void onItemDel(View view, E e);

        void onItemEdit(View view, E e);

        void onItemDelCompleted();
    }

}
